import md5 from 'crypto-js/md5';

export default{
    data(){
        return{
            data1:[] ,
            data:{
                limit:10,
                page:1,
                pages:0,
                total:0
              },
            modal1 : false,
            modal2 : false,
            
            num:0,
            ids:[],
            nsearch:''
        }
    },
    created(){
        this.getData()
      },
    methods:{
        
        
          remove (data) {
            this.$http({
             method:'delete',
             url:`http://10.31.153.25:3000/${this.modal3}/data/${data.row._id}`
           }).then(res=>{
             this.getData();
             this.success(true,"用户删除")
           })
           
        },
        
        ok (name) {
            this.$Message.info('点击了确定');

        },
        cancel () {
            this.$Message.info('点击了取消');
        },
        handleSubmit (name,type) {
           this.formValidate.value = this.type;
           console.log(this.formValidate.value)
              this.$refs[name].validate((valid) => {
                  if (valid) {
                    this.formValidate.password = md5(this.formValidate.password).toString();
                      this.$http({
                        method: 'post',
                        url:`http://10.31.153.25:3000/${this.modal3}/data`,
                        data: this.formValidate
                      }).then(res=>{
                        this.getData();
                        this.success(true,"添加");
                        this.modal2=false
                        console.log(this.formValidate)

                       // 重置form表单内容为数据的初始值状态
                       // ********** 非常重要
                       Object.assign(this.$data.formValidate, 
                       this.$options.data().formValidate);


                      })    
                  } else {
                      console.log(22222)
                      this.warning(true,"添加")
                  }
              })
          },
          handleReset (name) {
             this.$refs[name].resetFields();
             this.$Message.success('重置成功');
         },
         onSelectChange(node){
            console.log(node[0]._id);
            this.data.cateId=node[0]._id;
            this.formValidate.cateId=node[0]._id;
            
            this.getData();
        },
        onChangePage(page){
            console.log(page)
          this.$http({
            method:'post',
            url:`http://10.31.153.25:3000/${this.modal3}/list`,
            data:{
              limit: this.data.limit,
              page:page
            }

          }).then(res=>{
            this.data1 = res.data.docs;
            this.data.total = res.data.total;
          })
        },
        success (nodesc,name) {
            this.$Notice.success({
                title: name+'成功',
                desc: nodesc ? '' : '成功'
            });
        },
        warning (nodesc,name) {
            this.$Notice.warning({
                title: name+'失败',
                desc: nodesc ? '' : 'Here is the notification description. Here is the notification description. '
            });
        },            
        handleSelectAll (status) {
            this.$refs.selection.selectAll(status);
        },        
        removes(){
            this.$http({
                    method:'post',
                    url:`http://10.31.153.25:3000/${this.modal3}/data/removes`,
                    data:{
                        ids:this.ids
                    }

                }).then(res=>{
                    this.success(true,"删除")
                    this.getData()
                })
        },
        onSelectionChange(selection){
            if(selection && selection.length>0){
                var ids=[]
                for(let i=0;i<selection.length;i++){
                    ids.push(selection[i]._id)
                }
                this.ids = ids
                
            }
        },
        Search(){
            this.getData(this.nsearch)
            console.log(this.nsearch)
       },
        updata(data){
            console.log(data)
            this.$http({
                      method: 'put',
                      url:`http://10.31.153.25:3000/${this.modal3}/data/${data._id}`,
                      data: data
                    }).then(res=>{
                      this.getData();
                      this.success(true,"更新")
                        this.modal1=false
                     // 重置form表单内容为数据的初始值状态
                     // ********** 非常重要
                     Object.assign(this.$data.formValidate, 
                     this.$options.data().formValidate);


                    }) 
        },
    }
}